<template>
  <div class="wrapper wrapper-content animated fadeInRight v-cloak" v-cloak>
    <div class="row">
      <div class="col-md-12">
        <form id="info_search_form" role="form" class="form-inline" method="post" accept-charset="UTF-8" @submit.prevent="page(1)">
          <vue-area-4
              areaId="areaId"
              areaName="areaName"
              :areaIdVal="staff.dto().areaId"
              :areaNameVal="staff.dto().areaName"
              :areaFixed="staff.dto().areaId"
              :selectBy="staff.dto().areaId"
          ></vue-area-4>
          <div class="form-group">
            <input type="text" name="keyword" placeholder="请输入关键词" class="input-sm form-control">
          </div>
          <div class="form-group">
            <button type="submit" class="btn btn-sm btn-primary">搜索</button>
            <button type="reset" class="btn btn-sm btn-dafault">重置</button>
          </div>
        </form>
        <a href="javascript:;" @click="exportXls()" x-if='staff.hasPermission("analysis:river:supervisor:export")' class="btn btn-info m-t"><i class="fa fa-file-excel-o"></i> 导出数据</a>
        <div class="table-responsive table-scrollable">
          <table class="table table-bordered table-striped  table-hover">
            <thead>
            <tr>
              <th style="width:20px;">#</th>
              <th>行政区划</th>
              <th>县级河长</th>
              <th>乡镇级河长</th>
              <th>村级河长</th>
            </tr>
            </thead>
            <tbody>
            <tr v-if="result.data.data && result.data.data.length>0" v-for="(item, index) in result.data.data">
              <td>{{ (result.data.pageIndex - 1) * result.data.pageSize + index + 1 }}</td>
              <td>{{ item.areaName }}</td>
              <td>{{ item.level3Count }}</td>
              <td>{{ item.level4Count }}</td>
              <td>{{ item.level5Count }}</td>
            </tr>
            <tr v-if="result.data.data==null || result.data.data.length==0">
              <td colspan="5">
                <div class="alert"><strong>{{ result | message }}</strong></div>
              </td>
            </tr>
            </tbody>
          </table>
          <div class="b_page" v-html="tagPages"></div>
        </div>
      </div>
      <div class="col-md-12">

        <div class="col-sm-6">
          <div class="echarts" id="echarts-pie-chart-AreaLevel"></div>
        </div>

        <div class="col-sm-6">
          <div class="echarts" id="echarts-pie-chart-AreaLevelreaName"></div>
        </div>
      </div>
    </div>
  </div>
</template>
<style>
.echarts {
  height: 540px;
}
</style>
<script type="text/javascript">
import '@/assets/css/style.css'

import $ from '@/assets/js/jquery-vendor.js'
import 'jquery.cookie'
import axios from 'axios'
import {$storage, axiosContentType, site} from '@/assets/js/boss'
import '@/assets/js/pageTag'
import echarts from 'echarts'
import vueArea4 from '@/components/vue-area-4'
import Swal from 'sweetalert2'

var pieChartAreaName;
var pieoptionAreaName;
var pieChartAreaLevel;
var pieoptionAreaLevel;
export default {
  components: {
    'vue-area-4': vueArea4
  },
  data() {
    return {
      result: {data: {pageIndex: 1, data: []}, message: ''},
      tagPages: ''
    }
  },
  mounted() {
    let that = window.$vueApp = this;
    document.getElementsByTagName('body')[0].className = 'fixed-sidebar full-height-layout';
    this.staff.init(function () {
      that.initChart();
      that.page(1);
      that.areaLevel();
    });
  },
  methods: {
    initChart() {
      pieChartAreaLevel = echarts.init(document.getElementById('echarts-pie-chart-AreaLevel'));
      pieoptionAreaLevel = {
        title: {
          text: '各级河长统计',
          subtext: '',
          x: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b}({c})'
        },
        legend: {
          orient: 'vertical',
          x: 'right',
          top: '100px',
          data: ['县级', '乡镇级', '村级']
        },
        calculable: true,
        series: {
          name: '',
          type: 'pie',
          radius: '55%',
          center: ['50%', '60%'],
          data: []
        }
      };
      $(window).resize(pieChartAreaLevel.resize);
      pieChartAreaName = echarts.init(document.getElementById('echarts-pie-chart-AreaLevelreaName'));
      pieoptionAreaName = {
        title: {
          text: '次级行政区河长统计'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: []
        },
        grid: {
          x: 30,
          x2: 40,
          y2: 24
        },
        calculable: true,
        xAxis: {
          type: 'category',
          data: []
        },
        yAxis: {
          type: 'value'
        },
        series: {
          name: '',
          type: 'bar',
          data: []
        }
      }
      $(window).resize(pieChartAreaName.resize);
    },
    page(pageIndex) {
      let that = this;
      if (!pageIndex) pageIndex = 1;
      if (pageIndex === 1) {
        that.areaName();
      }
      axios.post(site.analysis.riverSupervisor.areaPage + pageIndex
          , $('#info_search_form').serialize()
          , axiosContentType.xWwwFormUrlencoded
      ).then(function (response) {
        const result = response.data;
        if (result.code === 0) {
          that.result = result;
          that.tagPages = that.tag.page(result.data);
        } else {
          Swal.fire(result.message);
        }
      });
    },
    exportXls() {
      let formJson = $('#info_search_form').serializeObject();
      // console.info(formJson)
      var formHtmlStr = `<form method="post" action="${site.analysis.riverSupervisor.exportXls}">
                               <input type="hidden" name="Authorization" value="${$storage.getItem('authorizationToken')}">`;
      for (let key in formJson) {
        // console.info(key)
        formHtmlStr += `<input type="hidden" name="${key}" value="${formJson[key]}">`;
      }
      formHtmlStr += `</form>`;
      // console.info(formHtmlStr)
      var $eleForm = $(formHtmlStr);
      $(document.body).append($eleForm);
      // 提交表单，实现下载
      $eleForm.submit().remove();
    },
    areaLevel() {
      // let that = this;
      axios.post(site.analysis.riverSupervisor.areaLevel
          , $('#info_search_form').serialize()
          , axiosContentType.xWwwFormUrlencoded
      ).then(function (response) {
        const result = response.data;
        if (result.code === 0) {
          let dataItems = result.data;
          if (dataItems && dataItems.length > 0) {
            // console.info(pieoptionAreaLevel.series.data)
            for (var dataItem of dataItems) {
              pieoptionAreaLevel.series.data.push({value: dataItem.value, name: dataItem.name + '级'});
            }
          } else {
            pieoptionAreaLevel.series.data = [null];
          }
          pieChartAreaLevel.setOption(pieoptionAreaLevel);
        } else {
          Swal.fire(result.message);
        }
      });
    },
    areaName() {
      pieoptionAreaName.xAxis.data = [];
      pieoptionAreaName.series.data = [];
      // let that = this;
      axios.post(site.analysis.riverSupervisor.areaName
          , $('#info_search_form').serialize()
          , axiosContentType.xWwwFormUrlencoded
      ).then(function (response) {
        const result = response.data;
        if (result.code === 0) {
          let dataItems = result.data;
          if (dataItems && dataItems.length > 0) {
            // console.info(pieoptionAreaName.series.data)
            for (var dataItem of dataItems) {
              let _name = dataItem.name;
              let _value = dataItem.value;
              pieoptionAreaName.xAxis.data.push(_name);
              pieoptionAreaName.series.data.push({value: _value, name: _name});
            }
          } else {
            pieoptionAreaName.xAxis.data = ['无'];
            pieoptionAreaName.series.data = [{value: 0, name: '无'}];
          }
          pieChartAreaName.setOption(pieoptionAreaName);
        } else {
          Swal.fire(result.message);
        }
      });
      //
      // for (let i = 0; i < 50; i++) {
      //   pieoptionAreaName.legend.data.push('_name' + i);
      //   pieoptionAreaName.series.data.push({value: 1, name: '_name' + i});
      //   pieChartAreaName.setOption(pieoptionAreaName);
      // }
    }
  }
}
</script>
